package com.twu.biblioteca;

import com.twu.biblioteca.Session;
import com.twu.biblioteca.menuChoice;
import com.twu.biblioteca.notValidMenuOptionException;

import java.util.*;
/**
 * Created with IntelliJ IDEA.
 * User: kush jain
 * Date: 6/28/13
 * Time: 5:17 PM
 * To change this template use File | Settings | File Templates.
 */
public class menu {
    ArrayList menuOptions;
    int choiceCount;
    menu()
    {
        menuOptions = new ArrayList();
    }

    public void setMenuOptions()
    {
        int index = 1;
        if(Session.isAdmin)
        {
            menuOptions.add(new menuChoice(index++,"Add Book"));
            menuOptions.add(new menuChoice(index++,"Remove Book"));
            menuOptions.add(new menuChoice(index++,"Free Reserved Book"));
            menuOptions.add(new menuChoice(index++,"Add Movie"));
        }
        else
            menuOptions.add(new menuChoice(index++,"Login as Administrator"));
        menuOptions.add(new menuChoice(index++,"Show All Books"));
        menuOptions.add(new menuChoice(index++,"Show List of Movies"));
        menuOptions.add(new menuChoice(index++,"Search Book by Title"));
        menuOptions.add(new menuChoice(index++,"Reserve Book"));
        menuOptions.add(new menuChoice(index++,"Show Reserved Books"));
        menuOptions.add(new menuChoice(index++,"Check Membership Status"));
        menuOptions.add(new menuChoice(index++,"Exit"));
        choiceCount = index-1;
    }

    public String getMenuText()
    {
        String menuText = "";
        menuText = "\nMenu\n";
        for(int i=0;i<choiceCount;i++)
        {
          menuText = menuText + "\n\t" +  ((menuChoice)menuOptions.get(i)).toString() ;
        }
        return menuText;
    }

    public String getChoice(String choice)
    {
            String errorString = "Select a Valid option!!";
            try
            {
                int choiceNo = Integer.parseInt(choice);
                if(choiceNo >choiceCount || choiceNo < 0)
                    throw new notValidMenuOptionException();
                for(int i=0;i<choiceCount;i++)
                 {
                     if(((menuChoice)menuOptions.get(i)).optionNoEquals(choiceNo))
                     {
                           return ((menuChoice)menuOptions.get(i)).getLabel();
                     }
                 }

            }
            catch(Exception anyException)
            {
                  return errorString;
            }
          return errorString;
    }

}
